//
//  LoginController.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/15.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "LoginController.h"
#import "MONActivityIndicatorView.h"
#import "JFBStoreInfoDS.h"
#import "JFBGlobalSetting.h"
#import "MiPushSDK.h"
#import "UserXieYiViewController.h"
@interface LoginController ()<MONActivityIndicatorViewDelegate>
@property(nonatomic, strong) MONActivityIndicatorView *indicatorView;
@property (weak, nonatomic) IBOutlet UITextField *supplierNameField;
@property (weak, nonatomic) IBOutlet UITextField *password;
- (IBAction)doLogin:(id)sender;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.indicatorView = [[MONActivityIndicatorView alloc] init];
    self.indicatorView.numberOfCircles = 5;
    self.indicatorView.radius = 5;
    self.indicatorView.internalSpacing = 10;
    self.indicatorView.duration = 0.8;
    self.indicatorView.delay = 0.2;
    self.indicatorView.center = self.view.center;
    self.indicatorView.delegate = self;
    [self.view addSubview:self.indicatorView];
    
    UIButton *btn = (UIButton *)[self.view viewWithTag:15];
    btn.selected = YES;
    
    CALayer *loginBtn = self.loginButton.layer;
    [loginBtn setMasksToBounds:YES];
    [loginBtn setCornerRadius:5.0f];
    
    CALayer *userView = self.userNameView.layer;
    [userView setMasksToBounds:YES];
    [userView setBorderColor:UIColorFromRGB(0x999999).CGColor];
    [userView setBorderWidth:0.5f];
    [userView setCornerRadius:5.0f];
    
    CALayer *userRight = self.rightView.layer;
    [userRight setMasksToBounds:YES];
    [userRight setCornerRadius:5.0f];
    
    CALayer *passView = self.passWordView.layer;
    [passView setMasksToBounds:YES];
    [passView setBorderColor:UIColorFromRGB(0x999999).CGColor];
    [passView setBorderWidth:0.5f];
    [passView setCornerRadius:5.0f];
    
    CALayer *passRight = self.passRightView.layer;
    [passRight setMasksToBounds:YES];
    [passRight setCornerRadius:5.0f];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)doLogin:(id)sender {
    if ([NSString isNilOrEmptyOrWhitespace:self.supplierNameField.text]) {
        [JFBUIAnimation errorInputTips:self.supplierNameField animation:TipsBounces];
        return;
    }
    if ([NSString isNilOrEmptyOrWhitespace:self.password.text]) {
        [JFBUIAnimation errorInputTips:self.password animation:TipsBounces];
        return;
    }
    
    [self.indicatorView startAnimating];
    UIButton *btn = (UIButton *)[self.view viewWithTag:15];
    if (btn.selected == YES) {
        [JFBStoreInfoDS login:^(BOOL isSuccess, NSString *message, NSDictionary * data, NSError *error) {
            [self.indicatorView stopAnimating];
            
            if( isSuccess ) {
                UIViewController* viewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"OrderTabarContainer"];
                
                [self presentViewController:viewController animated:YES completion:^{}];
                
            } else {
                [JFBUIAnimation alertTips:message];
            }
        } username:self.supplierNameField.text passwod:self.password.text];
    }else{
        UIAlertView *userProtool = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请先同意用户协议" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [userProtool show];
    }
    
}

#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    return UIColorFromRGB(ApplicationMainColor);
}

- (IBAction)dismiss:(id)sender {
    [SVProgressHUD dismiss];
}
- (IBAction)webViewButton:(id)sender {
    UserXieYiViewController *xvc = [[UserXieYiViewController alloc]init];
    [self.navigationController pushViewController:xvc animated:YES];
}

- (IBAction)userXieYi:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn.selected == NO) {
        btn.selected=YES;
    }else if (btn.selected == YES){
        btn.selected = NO;
    }
}
@end
